package com.snpic.appaw.module.third.service.aliyun;

import com.aliyun.dypnsapi20170525.models.GetAuthTokenRequest;
import com.aliyun.dypnsapi20170525.models.GetAuthTokenResponse;
import com.aliyun.dypnsapi20170525.models.GetAuthTokenResponseBody;
import com.snpic.appaw.module.third.api.aliyun.dto.GetAuthTokenRequestDTO;
import com.snpic.appaw.module.third.api.aliyun.dto.GetAuthTokenResponseDTO;
import com.snpic.appaw.module.third.config.aliyun.PhoneNumberAuthenticationConfig;
import com.snpic.appaw.module.third.enums.ThirdConstant;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.bean.BeanUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import static com.snpic.appaw.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.snpic.appaw.module.third.enums.ErrorCodeConstants.ALIYUN_INTERFACE01_ERROR;

/**
 * @author JiJun
 * @date 2025/9/21 17:24
 * @修改时间 2025/9/21 17:24
 * @description 阿里云，号码认证服务实现类
 */
@Service
@Slf4j
public class PhoneNumberAuthenticationServiceImpl implements PhoneNumberAuthenticationService {
	@Resource
	private PhoneNumberAuthenticationConfig phoneNumberAuthenticationConfig;

	@Resource
	private com.aliyun.dypnsapi20170525.Client client;

	/**
	 * @param reqDTO
	 * @return
	 */
	@Override
	@Cacheable(cacheNames = ThirdConstant.ALIYUN_GETAUTHTOKEN_CACHE, key = "#reqDTO.oringin", unless = "#result == null")
	public GetAuthTokenResponseBody getAuthToken(GetAuthTokenRequestDTO reqDTO) throws Exception {
		GetAuthTokenRequest getCertifyResultRequest = new GetAuthTokenRequest()
			.setUrl(phoneNumberAuthenticationConfig.getUrl())
			.setOrigin(phoneNumberAuthenticationConfig.getOrigin());
		GetAuthTokenResponse describePhoneNumberResaleResp = client.getAuthToken(getCertifyResultRequest);
		GetAuthTokenResponseBody getAuthTokenResponseBody = describePhoneNumberResaleResp.body;
		String code = getAuthTokenResponseBody.code;
		if (!com.aliyun.teautil.Common.equalString(code, "OK")) {
			log.error("错误信息:" + getAuthTokenResponseBody.message + "");
			throw exception(ALIYUN_INTERFACE01_ERROR);
		}
		log.info("响应结果:" + getAuthTokenResponseBody + "");
//		GetAuthTokenResponseDTO getAuthTokenResponseDTO = BeanUtil.toBean(getAuthTokenResponseBody, GetAuthTokenResponseDTO.class);

		return getAuthTokenResponseBody;
	}
}
